package com.guiji.text2video.job.util;

/**
 * @Copyright (C), 2018-2021
 * @FileName: DingUtil
 * @Author: scl
 * @Date: 2021/11/22 16:02
 * @Description: 钉钉监控类
 */

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

public class DingUtil {

    public static void sendMsg(String content, String cookie) {


        try {
            //钉钉机器人地址（配置机器人的webhook）
            String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=4464908c42eced4d4e17938abebed1a2ca3a7d29c94f5e4d28a71226000da2a2";

            //是否通知所有人
            boolean isAtAll = true;
            //通知具体人的手机号码列表
            List<String> mobileList = Lists.newArrayList();

            //钉钉机器人消息内容
            content = content + ": " + cookie;
            //组装请求内容
            String reqStr = buildReqStr(content, isAtAll, mobileList);

            System.out.println(reqStr);
            //推送消息（http请求）
            HttpUtil.post(dingUrl, reqStr, 10000);

        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    /**
     * 组装请求报文
     *
     * @param content
     * @return
     */
    private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
        //消息内容
        Map<String, String> contentMap = Maps.newHashMap();
        contentMap.put("content", content);
        //通知人
        Map<String, Object> atMap = Maps.newHashMap();
        //1.是否通知所有人
        atMap.put("isAtAll", isAtAll);
        //2.通知具体人的手机号码列表
        atMap.put("atMobiles", mobileList);

        Map<String, Object> reqMap = Maps.newHashMap();
        reqMap.put("msgtype", "text");
        reqMap.put("text", contentMap);
        reqMap.put("at", atMap);

        return JSON.toJSONString(reqMap);
    }
}
